package com.taru.university.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taru.university.mapper.PhotoMapper;
import com.taru.university.model.Photo;
import com.taru.university.service.PhotoService;
import com.taru.university.utils.Constant;
import com.taru.university.utils.JsonResult;
import com.taru.university.model.Disease;
import com.taru.university.service.DiseaseService;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@CrossOrigin
public class DiseaseController {
    private static Logger logger = LoggerFactory.getLogger(DiseaseController.class);

    @Autowired
    DiseaseService diseaseService;
    /**
     * 查询病害科普表
     * @return
     */
    @RequestMapping("/query/disease/sort")
    @ResponseBody
    public Object queryDisease( @RequestParam(value = "diseaseSort") String diseaseSort,@RequestParam(value = "pageNum") int pageNum,
                                @RequestParam(value = "pageSize")   int pageSize
//           @RequestParam(value = "pageNum" ,required =false) Integer pageNum,
//           @RequestParam(value = "pageSize"  ,required =false) Integer pageSize
    ){
        JsonResult result = null;
        try {
            logger.info("查询类型：{}页数：{}页长：{}",diseaseSort,pageNum,pageSize);

            //int num = Integer.valueOf((String)pageNum);
           PageHelper.startPage(pageNum,pageSize);
           List<Disease> diseaseList = diseaseService.queryDisease(diseaseSort,pageNum,pageSize);
            PageInfo<Disease> pageInfo =new PageInfo<>(diseaseList);
            logger.info("查询结果：{}", pageInfo.getList().toString());
            result = new JsonResult ("查询成功", Constant.STATUC_SUCCESS, pageInfo.getList());
        }catch (Exception ex){
            logger.info(ex.getMessage());
            result = new JsonResult("查询异常", Constant.STATUC_FAILURE);
        }
        return result;
    }

    /**
     * 查询病害科普表
     * @return
     */
    @RequestMapping("/query/disease")
    @ResponseBody
    public Object queryDiseaseSort( String diseaseSort){
        JsonResult result = null;
        try {
            logger.info("查询类型：{}",diseaseSort);

            List<Disease> diseaseList = diseaseService.queryDiseaseSort(diseaseSort);
            PageInfo<Disease> pageInfo =new PageInfo<>(diseaseList);
            logger.info("查询结果：{}", pageInfo.getList().toString());
            result = new JsonResult ("查询成功", Constant.STATUC_SUCCESS, pageInfo.getList());
        }catch (Exception ex){
            logger.info(ex.getMessage());
            result = new JsonResult("查询异常", Constant.STATUC_FAILURE);
        }
        return result;
    }

    /**
     * 查询详情
     * @param diseaseId
     * @return
     */
    @RequestMapping("/query/disease/detail")
    @ResponseBody
    public Object queryDiseaseById(String diseaseId){
        JsonResult result = null;
        try {
            Disease disease = diseaseService.queryDiseaseById( diseaseId);
            logger.info("病害科普Id：{}", diseaseId);
            result = new JsonResult("查询成功", Constant.STATUC_SUCCESS,disease);
        }catch (Exception ex){
            logger.info(ex.getMessage());
            result = new JsonResult("查询异常", Constant.STATUC_FAILURE);
        }
        return result;
    }

    @RequestMapping("/insert/disease")
    @ResponseBody
    public Object insertDisease(Disease disease){
        JsonResult result = null;
        try {
             diseaseService.insertDisease(disease);
            logger.info("数据：{}",  disease);
            result = new JsonResult("添加成功", Constant.STATUC_SUCCESS);
        }catch (Exception ex){
            logger.info(ex.getMessage());
            result = new JsonResult("添加异常", Constant.STATUC_FAILURE);
        }
        return result;
    }

    @RequestMapping("/delete/disease")
    @ResponseBody
    public Object deleteDiseaseControl(String diseaseId){
        JsonResult result = null;
        try {
            diseaseService.deleteDisease(diseaseId);
            logger.info("病害科普Id：{}",  diseaseId);
            result = new JsonResult("查询成功", Constant.STATUC_SUCCESS);
        }catch (Exception ex){
            logger.info(ex.getMessage());
            result = new JsonResult("查询异常", Constant.STATUC_FAILURE);
        }
        return result;
    }

    @RequestMapping("/query/disease/tetle")
    @ResponseBody
    public Object queryDiseaseByTitle(@RequestParam(value = "diseaseTitle")String diseaseTitle,@RequestParam(value = "pageNum") int pageNum,
                                      @RequestParam(value = "pageSize")   int pageSize){
        JsonResult result = null;
        try {
            List<Disease> diseaseList = diseaseService.queryDiseaseByTitle(diseaseTitle,pageNum,pageSize);
            logger.info("查询标题：{}",diseaseTitle);
            result = new JsonResult("查询成功", Constant.STATUC_SUCCESS,diseaseList);
        }catch (Exception ex){
            logger.info(ex.getMessage());
            result = new JsonResult("查询异常", Constant.STATUC_FAILURE);
        }
        return result;
    }

    @RequestMapping("/query/disease/tetle/len")
    @ResponseBody
    public Object queryDiseaseByTitleLen(String diseaseTitle){
        JsonResult result = null;
        try {
            List<Disease> diseaseList = diseaseService.queryDiseaseByTitleLen(diseaseTitle);
            logger.info("查询标题：{}",diseaseTitle);
            result = new JsonResult("查询成功", Constant.STATUC_SUCCESS,diseaseList);
        }catch (Exception ex){
            logger.info(ex.getMessage());
            result = new JsonResult("查询异常", Constant.STATUC_FAILURE);
        }
        return result;
    }

    @RequestMapping("/query/disease/statue")
    @ResponseBody
    public Object queryDiseaseByStatue(@RequestParam(value = "pageNum") int pageNum,
                                       @RequestParam(value = "pageSize") int pageSize){
        JsonResult result = null;
        try {
            List<Disease> diseaseList = diseaseService.queryDiseaseByStatue(pageNum,pageSize);
            logger.info("查询状态为0");
            logger.info("查询结果{}", diseaseList.toString());
            result = new JsonResult("查询成功", Constant.STATUC_SUCCESS,diseaseList);
        }catch (Exception ex){
            logger.info(ex.getMessage());
            result = new JsonResult("查询异常", Constant.STATUC_FAILURE);
        }
        return result;
    }

    @RequestMapping("/query/disease/statue/len")
    @ResponseBody
    public Object queryDiseaseByStatueLen(){
        JsonResult result = null;
        try {
            List<Disease> diseaseList = diseaseService.queryDiseaseByStatueLen();
            logger.info("查询状态为0");
            logger.info("查询结果{}", diseaseList.toString());
            result = new JsonResult("查询成功", Constant.STATUC_SUCCESS,diseaseList);
        }catch (Exception ex){
            logger.info(ex.getMessage());
            result = new JsonResult("查询异常", Constant.STATUC_FAILURE);
        }
        return result;
    }

}
